package com.example.nkj.timer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.util.Timer;
import java.util.TimerTask;

public class timer extends AppCompatActivity {

    private ProgressBar progressBar;
    private ToggleButton togglebutton;
    private Switch mswitch;
    private Timer timer =null;
    private TimerTask timerTask = null;
    private int progress =0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setMax(100);
        progressBar.setProgress(0);

        togglebutton = (ToggleButton)findViewById(R.id.toggleButton);
        togglebutton.setOnCheckedChangeListener(toggleButtonOnCheckedChangeListener);

        mswitch = (Switch)findViewById(R.id.mswitch);
        mswitch.setOnCheckedChangeListener(mswitchOnCheckedChangeListener);
    }

    private ToggleButton.OnCheckedChangeListener toggleButtonOnCheckedChangeListener = new ToggleButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if(isChecked)
            {
                startTimer();
                Toast.makeText(timer.this, "start timer", Toast.LENGTH_SHORT).show();
            }
            else
            {
                onPause();
                Toast.makeText(timer.this, "stop timer", Toast.LENGTH_SHORT).show();
            }
        }
    };

    private Switch.OnCheckedChangeListener mswitchOnCheckedChangeListener = new Switch.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if(isChecked)
            {
                startTimer();
                Toast.makeText(timer.this, "start timer", Toast.LENGTH_SHORT).show();
            }
            else
            {
                onPause();
                Toast.makeText(timer.this, "stop timer", Toast.LENGTH_SHORT).show();
            }
        }
    };

    protected void onPause()
    {
        super.onPause();
        stopTimer();
    }

    public void startTimer()
    {
        if(timer == null)
        {
            timer = new Timer();
            timerTask = new TimerTask()
            {
                @Override
                public void run()
                {
                    progress ++;
                    progressBar.setProgress(progress%101);
                }
            };
            timer.schedule(timerTask,50,10);
        }
    }

    public void stopTimer()
    {
        if(timer != null)
        {
            timer.cancel();
            timer = null;
            timerTask.cancel();
            timerTask = null;
        }
    }
}
